package org.apache.http.message;

import java.util.BitSet;
import org.apache.http.a.d;
import org.apache.http.annotation.Immutable;

@Immutable
public class k {
    public static final k a = new k();

    public static BitSet a(int... iArr) {
        BitSet bitSet = new BitSet();
        for (int i : iArr) {
            bitSet.set(i);
        }
        return bitSet;
    }

    public static boolean a(char c) {
        return c == ' ' || c == '\t' || c == '\r' || c == '\n';
    }

    public String a(d dVar, j jVar, BitSet bitSet) {
        StringBuilder stringBuilder = new StringBuilder();
        Object obj = null;
        while (!jVar.c()) {
            char charAt = dVar.charAt(jVar.b());
            if (bitSet != null && bitSet.get(charAt)) {
                break;
            } else if (a(charAt)) {
                a(dVar, jVar);
                obj = 1;
            } else {
                if (obj != null && stringBuilder.length() > 0) {
                    stringBuilder.append(' ');
                }
                a(dVar, jVar, bitSet, stringBuilder);
                obj = null;
            }
        }
        return stringBuilder.toString();
    }

    public void a(d dVar, j jVar) {
        int b = jVar.b();
        int b2 = jVar.b();
        int a = jVar.a();
        while (b2 < a && a(dVar.charAt(b2))) {
            b++;
            b2++;
        }
        jVar.a(b);
    }

    public void a(d dVar, j jVar, StringBuilder stringBuilder) {
        if (!jVar.c()) {
            int b = jVar.b();
            int b2 = jVar.b();
            int a = jVar.a();
            if (dVar.charAt(b) == '\"') {
                int i = b + 1;
                b2++;
                Object obj = null;
                while (b2 < a) {
                    char charAt = dVar.charAt(b2);
                    if (obj != null) {
                        if (!(charAt == '\"' || charAt == '\\')) {
                            stringBuilder.append('\\');
                        }
                        stringBuilder.append(charAt);
                        obj = null;
                    } else if (charAt == '\"') {
                        b = i + 1;
                        break;
                    } else if (charAt == '\\') {
                        obj = 1;
                    } else if (!(charAt == '\r' || charAt == '\n')) {
                        stringBuilder.append(charAt);
                    }
                    b2++;
                    i++;
                }
                b = i;
                jVar.a(b);
            }
        }
    }

    public void a(d dVar, j jVar, BitSet bitSet, StringBuilder stringBuilder) {
        int b = jVar.b();
        int a = jVar.a();
        for (int b2 = jVar.b(); b2 < a; b2++) {
            char charAt = dVar.charAt(b2);
            if ((bitSet != null && bitSet.get(charAt)) || a(charAt)) {
                break;
            }
            b++;
            stringBuilder.append(charAt);
        }
        jVar.a(b);
    }

    public String b(d dVar, j jVar, BitSet bitSet) {
        StringBuilder stringBuilder = new StringBuilder();
        Object obj = null;
        while (!jVar.c()) {
            char charAt = dVar.charAt(jVar.b());
            if (bitSet != null && bitSet.get(charAt)) {
                break;
            } else if (a(charAt)) {
                a(dVar, jVar);
                obj = 1;
            } else if (charAt == '\"') {
                if (obj != null && stringBuilder.length() > 0) {
                    stringBuilder.append(' ');
                }
                a(dVar, jVar, stringBuilder);
                obj = null;
            } else {
                if (obj != null && stringBuilder.length() > 0) {
                    stringBuilder.append(' ');
                }
                b(dVar, jVar, bitSet, stringBuilder);
                obj = null;
            }
        }
        return stringBuilder.toString();
    }

    public void b(d dVar, j jVar, BitSet bitSet, StringBuilder stringBuilder) {
        int b = jVar.b();
        int a = jVar.a();
        for (int b2 = jVar.b(); b2 < a; b2++) {
            char charAt = dVar.charAt(b2);
            if ((bitSet != null && bitSet.get(charAt)) || a(charAt) || charAt == '\"') {
                break;
            }
            b++;
            stringBuilder.append(charAt);
        }
        jVar.a(b);
    }
}
